﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;

namespace Planetwork.Admin
{
    public partial class ReviewManager : System.Web.UI.Page
    {
        public static string conStr = ConfigurationManager.AppSettings["conStr"];
        public static SqlConnection mycon = new SqlConnection(conStr);
        public static int pageSize = 8;
        protected void Page_Load(object sender, EventArgs e)
        {        
            if(!IsPostBack)
            {
                BindData(1);
            }

            int totalPage = TotalPage();

            //动态增加按钮
            for (int i = 1; i <= totalPage; i++)
            {
                var btn = new Button();
                btn.Text = i.ToString();
                btn.Click += new EventHandler(Paging);
                btn.CssClass = "btn btn-default";          
                this.pagingBtnGroup.Controls.Add(btn);         
            }
        }


        /// <summary>
        /// 提供数据源
        /// </summary>
        public void BindData(int currentPage)
        {
            int skip = pageSize * currentPage - pageSize;
            string sql = "select top " + pageSize + " *from Reviews where ID not in(select top " + skip + " ID from Reviews)";
            SqlDataAdapter mydat = new SqlDataAdapter(sql, mycon);
            DataSet myds = new DataSet();

            mycon.Open();
            mydat.Fill(myds);
            mycon.Close();

            if (myds.Tables[0].Rows.Count > 0)
            {
                this.showReview.DataSource = myds.Tables[0];
                this.showReview.DataBind();
            }
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void showReview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string sql = "delete Reviews where id=" + e.CommandArgument.ToString();
                SqlCommand mycmd = new SqlCommand(sql, mycon);

                mycon.Open();
                int i = mycmd.ExecuteNonQuery();
                mycon.Close();

                if (i > 0)
                {
                    Response.Write("<script>alert('删除成功');</script>");
                    BindData(1);
                }
                else
                {
                    Response.Write("<script>alert('删除失败');</script>");
                }
            }
        }

        /// <summary>
        /// 分页总数
        /// </summary>
        /// <returns></returns>
        public static int TotalPage()
        {
            SqlDataAdapter mydat = new SqlDataAdapter("select ID from Reviews", mycon);
            DataSet myds = new DataSet();

            mycon.Open();
            mydat.Fill(myds);
            mycon.Close();

            int i = myds.Tables[0].Rows.Count;
            i = (int)Math.Ceiling((decimal)i / pageSize);
            return i;
        }

        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Paging(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            int currentPage;
            if (btn.Text == "首页")
                currentPage = 1;
            else if (btn.Text == "末页")
                currentPage = TotalPage();
            else
            {
                currentPage = Convert.ToInt32(btn.Text);
                
            }
            
            BindData(currentPage);
            
        }
    }
}